package elcom.model.common;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import org.jconfig.ConfigurationManager;
import org.jconfig.handler.XMLFileHandler;

public class Appconfig {
	public void loadConfig (){
		ConfigurationManager cm = ConfigurationManager.getInstance();
		try {
			File file = new File(Constant.CONFIG_FOLDER);
			if (!file.exists()) {
				file.mkdirs();
			}
			
			file = new File(Constant.CONFIG_FOLDER + "ElcomInputOutput");
			if (!file.exists()) {
				createDefaultConfigFile(file);
			}
			
			XMLFileHandler fh = new XMLFileHandler();
			fh.setFile(file);
			cm.load(fh, "ElcomInputOutput");
		} catch (Exception e) {
			e.printStackTrace();
			Log.logErr(e.toString());
		}
	}
	
	private void createDefaultConfigFile(File file) throws Exception {
		BufferedWriter bw = new BufferedWriter(new FileWriter(file));
		bw.write("<?xml version=\"1.0\" ?>");
        bw.newLine();
        bw.write("\t<properties>");
        bw.newLine();
        bw.write("\t\t<category name=\"JDBC\">");
        bw.newLine();
        bw.write("\t\t\t<property name=\"URL\" value=\"jdbc:oracle:thin:@(description=(address=(host=192.168.6.112)(protocol=tcp)(port=1521))(connect_data=(SERVER = DEDICATED)(service_name=gstt)))\"/>");
        bw.newLine();
        bw.write("\t\t\t<property name=\"USERNAME\" value=\"dv\"/>");
        bw.newLine();
        bw.write("\t\t\t<property name=\"PASSWORD\" value=\"dv\"/>");
        bw.newLine();
        bw.write("\t\t\t<property name=\"MAX_LIMIT\" value=\"4\"/>");
        bw.newLine();
        bw.write("\t\t\t<property name=\"AUTO_COMMIT\" value=\"false\"/>");
        bw.newLine();
        bw.write("\t\t</category>");
        bw.newLine();
        bw.write("\t</properties>");
        bw.flush();
        bw.close();
	}
}
